package com.ruoyi.datastatistics.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.datastatistics.domain.DosageTrendData;
import com.ruoyi.datastatistics.domain.TerminalHistoryData;
import com.ruoyi.datastatistics.mapper.TerminalDbStatisticAnalysisMapper;
import com.ruoyi.datastatistics.service.ITerminalDbStatisticAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TerminalDbStatisticAnalysisServiceImpl extends BaseController implements ITerminalDbStatisticAnalysisService {

    @Autowired
    private TerminalDbStatisticAnalysisMapper terminalDbStatisticAnalysisMapper;

    @Override
    public Map<String, Object> oneTerminal(String[] location_nb, String[] readDates) {
        String location = location_nb[0];
        Map<String, Object> maps = new HashMap<String, Object>();
        for(String day:readDates){
            String startTime = day+" 00:00:00";
            String endTime = day+" 23:59:59";
            List<TerminalHistoryData> list = terminalDbStatisticAnalysisMapper.getTerminalDbHistory(location,startTime,endTime);
            maps.put(day,list);
        }
        return maps;
    }
    @Override
    public JSONArray oneTerminal2(String[] location_nb, String[] readDates, String[] time, int type) {
        JSONArray json = new JSONArray();
        String location = location_nb[0];
        Map<String, Object> maps = new HashMap<String, Object>();
        int i= 0 ;
        for(String day:readDates){
            JSONObject jo = new JSONObject();
            String startTime = day +" "+ time[0];
            String endTime = day +" "+ time[1];
            List<TerminalHistoryData> list = terminalDbStatisticAnalysisMapper.getTerminalDbHistory(location,startTime,endTime);
            maps.put(day,list);
            jo.put("index", i);
            jo.put("date", readDates[i]);
            list = list.stream().sorted(Comparator.comparing(TerminalHistoryData::getReadDate)).collect(Collectors.toList());
            Object[] dates = list.stream().map(e->e.getReadDate().toString().substring(11,16)).collect(Collectors.toList()).toArray();
            String  dateinfo = (list==null || list.size()==0)? "": String.join(",",convertToStrings(dates));
            jo.put("dateInfo", dateinfo);
           if (type == 1)
            {
                String  dataInfo = String.join(",",convertToStrings(list.stream().map(e->e.getPressure().toString()).collect(Collectors.toList()).toArray()));
                jo.put("dataInfo", dataInfo);
                jo.put("min", (list==null || list.size()==0)?"":list.stream().map(TerminalHistoryData::getPressure).max(BigDecimal::compareTo).get());
            }
            else
            {
                String  dataInfo = (list==null || list.size()==0)? "": String.join(",",convertToStrings(list.stream().map(e->e.getInstantNumber().toString()).collect(Collectors.toList()).toArray()));
                jo.put("dataInfo", dataInfo);
                jo.put("min", (list==null || list.size()==0)?"":list.stream().map(TerminalHistoryData::getInstantNumber).max(BigDecimal::compareTo).get());
            }
            json.add(jo);
            i++;
        }

        return json;
    }

    public static String[] convertToStrings(Object[] objects) {
        if (objects == null || objects.length == 0) {
            return new String[]{}; // 返回空字符串数组
        } else {
            String[] strings = new String[objects.length];

            for (int i = 0; i < objects.length; i++) {
                strings[i] = String.valueOf(objects[i]);
            }

            return strings;
        }
    }

    @Override
    public Map<String, Object> multiTerminal(String[] location_nb, String[] readDates) {
        String day = readDates[0];
        String startTime = day+" 00:00:00";
        String endTime = day+" 23:59:59";
        Map<String, Object> maps = new HashMap<String, Object>();
        for(String location:location_nb){
            List<TerminalHistoryData> list = terminalDbStatisticAnalysisMapper.getTerminalDbHistory(location,startTime,endTime);
            maps.put(location,list);
        }
        return maps;
    }

    @Override
    public List<DosageTrendData> dosageTrend(String location_nb,String startTime,String endTime,String interval,String dataType){
        List<DosageTrendData> list =  new ArrayList<DosageTrendData>();
        switch (dataType){
            case "1":  //时报表
                list = terminalDbStatisticAnalysisMapper.getDataTrendHour(location_nb, interval, startTime, endTime);
                break;
            case "2"://日报表
                list = terminalDbStatisticAnalysisMapper.getDataTrendDay(location_nb, startTime, endTime);
                break;
            case "3"://月报表
                list = terminalDbStatisticAnalysisMapper.getDataTrendMonth(location_nb, startTime, endTime);
                break;
        }

        return list;
    }


    @Override
    public TableDataInfo dosageTrendForPage(String location_nb, String startTime, String endTime, String interval, String dataType){

        List<DosageTrendData> list =  new ArrayList<DosageTrendData>();
        switch (dataType){
            case "1":  //时报表
                Integer counts = terminalDbStatisticAnalysisMapper.getDataTrendHour(location_nb, interval, startTime, endTime).size();
//                startPageNoCount();
                list = terminalDbStatisticAnalysisMapper.getDataTrendHour(location_nb, interval, startTime, endTime);
                return getDataTableNoCount(list,counts);
            case "2"://日报表
//                startPage();
                list = terminalDbStatisticAnalysisMapper.getDataTrendDay(location_nb, startTime, endTime);
                return getDataTable(list);
            case "3"://月报表
//                startPage();
                list = terminalDbStatisticAnalysisMapper.getDataTrendMonth(location_nb, startTime, endTime);
                return getDataTable(list);
        }

        return getDataTable(list);
    }
}
